package com.ruoyi.mobile.project.ums.controller;

import com.ruoyi.db.pms.domain.PmsGoods;
import com.ruoyi.db.ums.domain.UmsFootprint;
import com.ruoyi.mobile.annotation.WxLoginUser;
import com.ruoyi.mobile.project.pms.service.WxPmsGoodsService;
import com.ruoyi.mobile.project.ums.service.WxUmsFootprintService;
import com.ruoyi.mobile.utils.JacksonUtil;
import com.ruoyi.mobile.utils.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户访问足迹服务
 */
@RestController
@RequestMapping("/wx/footprint")
@Validated
public class WxFootprintController
{
    private final Log logger = LogFactory.getLog(WxFootprintController.class);

    @Autowired
    private WxUmsFootprintService footprintService;

    @Autowired
    private WxPmsGoodsService goodsService;

    /**
     * 删除用户足迹
     *
     * @param userId 毛思淼
     * @param body   请求内容， { id: xxx }
     * @return 删除操作结果
     */
    @PostMapping("delete")
    public Object delete(@WxLoginUser Long userId, @RequestBody String body)
    {
        //判断用户是否存在
        if (userId == null)
        {
            return ResponseUtil.unlogin();
        }
        //判断要删除的是否存在
        if (body == null)
        {
            return ResponseUtil.badArgument();
        }

        Long footprintId = JacksonUtil.parseLong(body, "id");
        if (footprintId == null)
        {
            return ResponseUtil.badArgument();
        }
        //确定哪个用户和哪个id
        UmsFootprint footprint = footprintService.findById(userId, footprintId);

        if (footprint == null)
        {
            return ResponseUtil.badArgumentValue();
        }
        if (!footprint.getUserId().equals(userId))
        {
            return ResponseUtil.badArgumentValue();
        }
       //用户已确定，执行删除操作
        footprintService.deleteById(footprintId);
        return ResponseUtil.ok();
    }

    /**
     * 用户足迹列表
     *
     * @param page  毛思淼
     * @param limit 分页大小
     * @return 用户足迹列表
     */
    @GetMapping("list")
    public Object list(@WxLoginUser Long userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit)
    {
        if (userId == null)
        {
            return ResponseUtil.unlogin();
        }

        //查询userId用户的所有浏览对象实体
        List<UmsFootprint> footprintList = footprintService.queryByAddTime(userId, page, limit);

        List<Object> footprintVoList = new ArrayList<>(footprintList.size());
        for (UmsFootprint footprint : footprintList)
        {
            Map<String, Object> c = new HashMap<String, Object>();
            c.put("id", footprint.getId());
            c.put("goodsId", footprint.getGoodsId());
            c.put("addTime", footprint.getAddTime());

            PmsGoods goods = goodsService.findById(footprint.getGoodsId());
            c.put("name", goods.getName());
            c.put("brief", goods.getBrief());
            c.put("picUrl", goods.getPicUrl());
            c.put("retailPrice", goods.getRetailPrice());

            footprintVoList.add(c);
        }

        return ResponseUtil.okList(footprintVoList, footprintList);
    }

}
